package com.ncr.test.pyramid.solver.impl;

import com.ncr.test.pyramid.data.Pyramid;
import com.ncr.test.pyramid.solver.PyramidSolver;

/**
 * TASK: This is your 2nd task.
 * Please implement the class to satisfy the interface. *
 */
public class YourSolver implements PyramidSolver {

	@Override
	public long pyramidMaximumTotal(Pyramid pyramid) {

		if (pyramid == null) {
			throw new IllegalArgumentException("Illegal input: pyramid is null");
		}

		int[][] pyramidData = pyramid.getData();

		if (pyramidData.length == 0 || pyramidData[0].length == 0) {
			throw new IllegalArgumentException("Illegal input: arrays are in illegal state");
		}

		for (int i = 1; i < pyramidData[0].length; i++) {
			for (int j = 0; j <= pyramidData[0].length-i-1; j++) {
				pyramidData[i][j] += Math.max(pyramidData[i - 1][j], pyramidData[i - 1][j + 1]);
			}
		}
		return pyramidData[pyramidData[0].length-1][0];

	}

}
